home *** CD-ROM | disk | FTP | other *** search
- function G_Alarm(callback, delayMS, opt_repeating, opt_maxTimes) {
- this.debugZone = "alarm";
- this.callback_ = callback;
- this.repeating_ = !!opt_repeating;
- var Cc = Components.classes;
- var Ci = Components.interfaces;
- this.timer_ = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
- var type = opt_repeating ?
- this.timer_.TYPE_REPEATING_SLACK :
- this.timer_.TYPE_ONE_SHOT;
- this.maxTimes_ = opt_maxTimes ? opt_maxTimes : null;
- this.nTimes_ = 0;
- this.timer_.initWithCallback(this, delayMS, type);
- }
- G_Alarm.prototype.cancel = function() {
- if (this.timer_) {
- this.timer_.cancel();
- this.timer_ = null;
- this.callback_ = null;
- }
- }
- G_Alarm.prototype.notify = function(timer) {
- var ret = this.callback_();
- this.nTimes_++;
- if (this.repeating_ &&
- typeof this.maxTimes_ == "number"
- && this.nTimes_ >= this.maxTimes_) {
- this.cancel();
- } else if (!this.repeating_) {
- this.cancel();
- }
- return ret;
- }
- G_Alarm.prototype.QueryInterface = function(iid) {
- if (iid.equals(Components.interfaces.nsISupports) ||
- iid.equals(Components.interfaces.nsIObserver) ||
- iid.equals(Components.interfaces.nsITimerCallback))
- return this;
- throw Components.results.NS_ERROR_NO_INTERFACE;
- }
- function G_ConditionalAlarm(callback, delayMS, opt_repeating, opt_maxTimes) {
- G_Alarm.call(this, callback, delayMS, opt_repeating, opt_maxTimes);
- this.debugZone = "conditionalalarm";
- }
- G_ConditionalAlarm.inherits(G_Alarm);
- G_ConditionalAlarm.prototype.notify = function(timer) {
- var rv = G_Alarm.prototype.notify.call(this, timer);
- if (this.repeating_ && rv) {
- G_Debug(this, "Callback of a repeating alarm returned true; cancelling.");
- this.cancel();
- }
- }
-